package io.zcoder.innoway.activities;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.models.Profile;
import io.zcoder.innoway.models.User;
import io.zcoder.innoway.widgets.EditTextCustom;
import io.zcoder.innoway.widgets.TextViewCustom;

public class SelectTallActivity extends BaseActivity {

    public static final int TYPE_CM = 0;
    public static final int TYPE_FT = 1;
    public static final int RESULT_SET_TALL = 23;

    @BindView(R.id.circle_ft)
    View mCircleFt;
    @BindView(R.id.circle_cm)
    View mCircleCm;
    @BindView(R.id.text_ft)
    TextViewCustom mTextFt;
    @BindView(R.id.text_cm)
    TextViewCustom mTextCm;
    @BindView(R.id.layout_input_cm)
    LinearLayout mLayoutInputCm;
    @BindView(R.id.layout_input_ft)
    LinearLayout mLayoutInputFt;
    @BindView(R.id.input_cm)
    EditTextCustom mInputCm;
    @BindView(R.id.input_ft)
    EditTextCustom mInputFt;
    @BindView(R.id.input_inch)
    EditTextCustom mInputInch;
    @BindView(R.id.next)
    TextViewCustom mNext;
    @BindView(R.id.txt_weight_unit)
    TextViewCustom mTxtWeghtUnit;
    @BindView(R.id.input_weight)
    EditTextCustom mInputWeight;


    @OnClick(R.id.next)
    void onNext() {
        float input = 0;
        float inputWeight = 0;
        if (mCurrentType == TYPE_CM) {
            try {
                input = Float.valueOf(mInputCm.getText().toString());
                inputWeight = Float.valueOf(mInputWeight.getText().toString());
            } catch (Exception e) {
                input = 0;
            }
        } else if (mCurrentType == TYPE_FT) {
            try {
                input = Float.valueOf(mInputFt.getText().toString());
                inputWeight = Float.valueOf(mInputFt.getText().toString());
                input = input * Constants.RATIO_FT_TO_CM;
                inputWeight = inputWeight / Constants.RATIO_KG_TO_LBS;
            } catch (Exception e) {
                input = 0;
            }
            try {
                float input2 = Float.valueOf(mInputInch.getText().toString());
                input = input + input2 * Constants.RATIO_INCH_TO_CM;
            } catch (Exception e) {
            }
        }
        if (input < 100 || input > 230) {
            showAlert(getString(R.string.error), getString(R.string.alert_input_height));
        } else {
            if (mFromEdit) {
                Intent intent = new Intent();

                intent.putExtra(Constants.TYPE_TALL_EXTRAS, mCurrentType);
                intent.putExtra(Constants.TALL_EXTRAS, input);
                intent.putExtra(Constants.INIT_WEIGHT_EXTRAS, inputWeight);
                setResult(RESULT_SET_TALL, intent);
                finish();
                return;
            }
            Intent intent = new Intent(this, SelectGenderActivity.class);
            Profile profile = new Profile();
            profile.setHeight((int)input);
            profile.setInitWeight(inputWeight);
            intent.putExtra(Constants.PROFILE_EXTRAS, profile);
            startActivity(intent);
        }
    }

    private int mCurrentType = TYPE_FT;
    private boolean mFromEdit;
    Profile mProfile;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_select_tall_activity);
        ButterKnife.bind(this);
        showBack(true);
        mInputCm.setSelection(mInputCm.getText().length());
        mInputInch.requestFocus();
        mInputInch.setSelection(mInputInch.getText().length());
        mFromEdit = getIntent().getBooleanExtra(Constants.EDIT_INFO_EXTRAS, false);
        if (mFromEdit) {
            mNext.setText(getString(R.string.save));
            mProfile = getIntent().getParcelableExtra(Constants.PROFILE_EXTRAS);
            if (mProfile != null) {
                mCurrentType = mProfile.getHeight_unit();
                if (mCurrentType == TYPE_CM) {
                    mInputCm.setText(mProfile.getHeight() + "");
                    mInputWeight.setText(String.format("%.1f", mProfile.getInitWeight()));
                    selectCm(null);
                } else {
                    mInputFt.setText(Utils.getOnlyFt(mProfile.getHeight()) + "");
                    mInputInch.setText(Utils.getOnlyInch(mProfile.getHeight()) + "");
                    mInputWeight.setText(String.format("%.1f", mProfile.getInitWeight() * Constants.RATIO_KG_TO_LBS));
                    selectFt(null);
                }
            }
        }
    }

    public void selectFt(View view) {
        mTextFt.setBackgroundResource(R.color.bg_select_tall_btn);
        mCircleFt.setBackgroundResource(R.drawable.circle_tabs_select);
        mTextCm.setBackgroundResource(R.color.bg_select_tall);
        mCircleCm.setBackgroundResource(R.drawable.circle_tabs);
        mCurrentType = TYPE_FT;
        mLayoutInputFt.setVisibility(View.VISIBLE);
        mLayoutInputCm.setVisibility(View.GONE);
        mTxtWeghtUnit.setText(R.string.lbs);
        mInputInch.requestFocus();
    }

    public void selectCm(View view) {
        mTextCm.setBackgroundResource(R.color.bg_select_tall_btn);
        mCircleCm.setBackgroundResource(R.drawable.circle_tabs_select);
        mTextFt.setBackgroundResource(R.color.bg_select_tall);
        mCircleFt.setBackgroundResource(R.drawable.circle_tabs);
        mCurrentType = TYPE_CM;
        mLayoutInputFt.setVisibility(View.GONE);
        mLayoutInputCm.setVisibility(View.VISIBLE);
        mTxtWeghtUnit.setText(R.string.kg);
        mInputCm.requestFocus();
    }
}
